package com.stanwind.sgit.util;

/**
 * ShellUtil shell工具集
 *
 * @author : Stan
 * @version : 1.0
 * @date :  2019-11-29 10:42
 **/
public class ShellUtil {
    public static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().contains("windows");
    private static final String GIT_CMD = "git";
    public static final String SHELL_CHARSET;

    static {
        if (IS_WINDOWS) {
            SHELL_CHARSET = "GBK";
        } else {
            String lang = System.getenv("LANG");
            if (StringUtils.isNotBlank(lang)) {
                if (lang.contains(".")) {
                    SHELL_CHARSET = lang.substring(lang.indexOf(".") + 1);
                } else {
                    SHELL_CHARSET = lang;
                }
            } else {
                SHELL_CHARSET = System.getProperty("file.encoding", "UTF-8");
            }
        }
    }

    private String buildCommand(String[] cmdArray) {
        StringBuilder builder = new StringBuilder();
        if (IS_WINDOWS) {
            builder.append("cmd /c \"");
        }
        for (int i = 0; i < cmdArray.length; i++) {
            if (i != 0) {
                builder.append(" && ");
            }
            builder.append(cmdArray[i]);
        }
        if (IS_WINDOWS) {
            builder.append(" \"");
        }

        return builder.toString();
    }
}
